local fenji = fk.CreateSkill {

  name = "joyex__fenji",

  tags = {  },

}



fenji:addEffect(fk.AfterCardsMove, {
  name = "joyex__fenji",
  anim_type = "support",
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(fenji.name) and player.hp >= 1 then
      for _, move in ipairs(data) do
        if (move.moveReason == fk.ReasonDiscard or move.moveReason == fk.ReasonPrey) then
          if move.from and move.proposer and move.from ~= move.proposer then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.PlayerHand then
                return true
              end
            end
          end
        end
      end
    end
  end,
  on_trigger = function(self, event, target, player, data)
    local room = player.room
    local targets = {}
    for _, move in ipairs(data) do
      if not table.contains(targets, move.from) and (move.moveReason == fk.ReasonDiscard or move.moveReason == fk.ReasonPrey) then
        if move.from and move.proposer and move.from ~= move.proposer then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand then
              table.insert(targets, move.from)
              break
            end
          end
        end
      end
    end
    room:sortPlayersByAction(targets)
    for _, target_id in ipairs(targets) do
      if not player:hasSkill(fenji.name) or player.hp < 1 then break end
      local skill_target = room:getPlayerById(target_id)
      if skill_target and not skill_target.dead then
        self:doCost(event, skill_target, player, data)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, fenji.name, nil, "#joyex__fenji-invoke::"..target.id)
  end,
  on_use = function(self, event, target, player, data)
    player.room:doIndicate(player.id, {target.id})
    player.room:loseHp(player, 1, fenji.name)
    if not target.dead then
      target:drawCards(2, fenji.name)
    end
  end,
})

return fenji